package com.aibili.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.Expinfo;
import com.aibili.pojo.FeedBack;
import com.aibili.pojo.FileInfo;
import com.aibili.pojo.LvInfo;
import com.aibili.pojo.MomentClick;
import com.aibili.pojo.MomentComment;
import com.aibili.pojo.MomentInfo;
import com.aibili.pojo.MomentShare;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.VideoPlay;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.daoshun.common.DsConstant;

@Service("monentService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class MomentService extends BaseService {

	/**
	 * 添加动态信息
	 * 
	 * @param momentinfo
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void SaveMomentInfo(MomentInfo momentinfo) {
		dataDao.addObject(momentinfo);
	}

	/**
	 * 根据id取动态信息
	 * 
	 * @param parseLong
	 * @return
	 */
	public MomentInfo getMonentInfo(long id) {
		MomentInfo info = dataDao.getObjectById(MomentInfo.class, id);
		return info;
	}

	/**
	 * 更新动态
	 * 
	 * @param momentinfo
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateMomentInfo(MomentInfo momentinfo) {
		dataDao.updateObject(momentinfo);
	}

	/**
	 * 添加动态评论
	 * 
	 * @param comment
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void SaveMomentComment(MomentComment comment) {
		dataDao.addObject(comment);
	}

	/**
	 * 根据动态id取动态评论
	 * 
	 * @param moment_id
	 *            动态id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MomentComment> getMomentCommentListByMomment(long moment_id, int page, int pageSize) {
		String hql = " from MomentComment where moment_id = :moment_id and is_delete <> 2 order by create_time desc";
		List<MomentComment> list = (List<MomentComment>) dataDao.pageQueryViaParam(hql, pageSize, page, new String[] { "moment_id" }, moment_id);
		return list;
	}

	/**
	 * 取所有动态
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MomentInfo> getAllMomentList() {
		String hql = " from MomentInfo order by create_time desc";
		List<MomentInfo> list = (List<MomentInfo>) dataDao.getObjectsViaParam(hql, null);
		return list;
	}

	/**
	 * 根据user_id 获取动态信息
	 * 
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MomentInfo> getMomentListByUserInfo(long user_id, int page, int pageSize) {
		String hql = " from MomentInfo where user.id = :user_id order by create_time desc";
		List<MomentInfo> list = (List<MomentInfo>) dataDao.pageQueryViaParam(hql, pageSize, page, new String[] { "user_id" }, user_id);
		if (list != null && list.size() > 0) {
			String hql2 = "select count(id) from MomentComment where moment_id = :moment_id and is_delete = 1 ";
			String hql4 = " from MomentClick where user_id = :user_id and moment_id = :moment_id ";
			for (MomentInfo momentInfo : list) {
				long count = (long) dataDao.getFirstObjectViaParam(hql2, new String[] { "moment_id" }, momentInfo.getId());
				momentInfo.setComment_num((int) count);

				MomentClick click = (MomentClick) dataDao.getFirstObjectViaParam(hql4, new String[] { "user_id", "moment_id" }, user_id, momentInfo.getId());
				if (click != null) {
					momentInfo.setIsclick(1);
				} else {
					momentInfo.setIsclick(0);
				}
			}
		}
		return list;
	}

	/**
	 * 分页取动态
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MomentInfo> getMomentListByPage(int page, int pageSize, long user_id) {
		StringBuffer hql = new StringBuffer(" from MomentInfo where 1=1");
		if (user_id != 0) {
			hql.append("and user_id =" + user_id + "order by create_time desc");
		} else {
			hql.append("and chioce = 1 order by top_time desc,create_time desc");
		}
		List<MomentInfo> list = (List<MomentInfo>) dataDao.pageQueryViaParam(hql.toString(), pageSize, page, null);
		if (list != null && list.size() > 0) {
			String hql2 = "select count(id) from MomentComment where moment_id = :moment_id and is_delete = 0 ";
			String hql4 = " from MomentClick where user_id = :user_id and moment_id = :moment_id ";

			for (MomentInfo momentInfo : list) {
				// 评论数
				long count = (long) dataDao.getFirstObjectViaParam(hql2, new String[] { "moment_id" }, momentInfo.getId());
				momentInfo.setComment_num((int) count);

				MomentClick click = (MomentClick) dataDao.getFirstObjectViaParam(hql4, new String[] { "user_id", "moment_id" }, user_id, momentInfo.getId());
				if (click != null) {
					momentInfo.setIsclick(1);
				} else {
					momentInfo.setIsclick(0);
				}
			}
		}
		return list;
	}

	/**
	 * 
	 * @Description 通过文件 id 获取文件信息
	 * @param id
	 * @return
	 */
	public FileInfo getFileInfoById(long id) {
		FileInfo info = dataDao.getObjectById(FileInfo.class, id);
		return info;
	}

	/**
	 * 
	 * @Description 判断用户是否点过赞
	 * @param user_id
	 * @param moment_id
	 * @return
	 */
	public MomentClick getMomentClickByUseridMomentid(long user_id, long moment_id) {
		String hql = " from MomentClick where user_id = :user_id and moment_id = :moment_id";
		MomentClick click = (MomentClick) dataDao.getFirstObjectViaParam(hql, new String[] { "user_id", "moment_id" }, user_id, moment_id);
		return click;
	}

	/**
	 * 
	 * @Description 新增点赞记录
	 * @param click
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addMomentClick(MomentClick click) {
		dataDao.addObject(click);
	}

	/**
	 * 取动态并判断是否点赞和评论数
	 * 
	 * @param id
	 * @param user_id
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public MomentInfo getMonentInfoCC(long id, long user_id) {
		MomentInfo info = dataDao.getObjectById(MomentInfo.class, id);
		String hql = "select count(id) from MomentComment where moment_id = :moment_id and is_delete=0";
		String hql2 = " from MomentClick where user_id = :user_id and moment_id = :moment_id";
		String hql3 = "select count(id) from MomentShare where moment_id = :moment_id";
		String hql4 = " from MomentShare where moment_id = :moment_id and user_id = :user_id";
		String hql5 = "from VideoPlay where user_id = :user_id and moment_id = :moment_id";
		String hql6 = "select count(id) from VideoPlay where moment_id = :moment_id";
		if (info != null) {
			// 评论数
			long count = (long) dataDao.getFirstObjectViaParam(hql, new String[] { "moment_id" }, info.getId());
			info.setComment_num((int) count);
			MomentClick click = (MomentClick) dataDao.getFirstObjectViaParam(hql2, new String[] { "user_id", "moment_id" }, user_id, info.getId());
			if (click != null) {
				info.setIsclick(1);
			} else {
				info.setIsclick(0);
			}
			VideoPlay play = (VideoPlay) dataDao.getFirstObjectViaParam(hql5, new String[] { "user_id", "moment_id" }, user_id, info.getId());
			if (play == null) {
				VideoPlay videoPlay = new VideoPlay();
				videoPlay.setUser_id(user_id);
				videoPlay.setMoment_id(info.getId());
				dataDao.addObject(videoPlay);
				long videocount = (long) dataDao.getFirstObjectViaParam(hql6, new String[] { "moment_id" }, info.getId());
				info.setPlaynum((int) videocount);// 播放数
			}

			long transpond_num = (long) dataDao.getFirstObjectViaParam(hql3, new String[] { "moment_id" }, info.getId());
			info.setTranspondnum((int) transpond_num);
			MomentShare share = (MomentShare) dataDao.getFirstObjectViaParam(hql4, new String[] { "moment_id", "user_id" }, info.getId(), user_id);
			if (share != null) {
				// info.setShare_url(share.getShare_url());//分享链接
			}

			// 加经验
			UserInfo to_user = info.getUser();

			long to_id = to_user.getId();
			String exphql = "select count(exp) from Expinfo where user_id = :user_id and DATE_FORMAT(NOW(),'%Y-%m-%d') = DATE_FORMAT(create_time,'%Y-%m-%d') " + "and type = 1";
			String exphql2 = "select count(exp) from Expinfo where user_id = :user_id and DATE_FORMAT(NOW(),'%Y-%m-%d') = DATE_FORMAT(create_time,'%Y-%m-%d') " + "and type = 2";
			long expto = (long) dataDao.getFirstObjectViaParam(exphql, new String[] { "user_id" }, to_id);
			long expfr = (long) dataDao.getFirstObjectViaParam(exphql2, new String[] { "user_id" }, user_id);
			if (expto < 300) {
				to_user.setExp(to_user.getExp() + 1);
				String lhql = " from LvInfo where lv = :lv ";
				LvInfo linfo = (LvInfo) dataDao.getFirstObjectViaParam(lhql, new String[] { "lv" }, to_user.getLevel());
				if (linfo != null && to_user.getExp() + 1 > (linfo.getExp() + linfo.getTotal())) {
					to_user.setLevel(to_user.getLevel() + 1);
				}
				Expinfo expinfo = new Expinfo();
				expinfo.setExp(1);
				expinfo.setUser_id(to_id);
				expinfo.setType(1);
				expinfo.setCreate_time(new Date());
				dataDao.addObject(expinfo);
			}
			UserInfo from_user = dataDao.getObjectById(UserInfo.class, user_id);
			if (from_user != null) {
				if (expfr < 5) {
					from_user.setExp(from_user.getExp() + 1);
					String lhql = " from LvInfo where lv = :lv ";
					LvInfo linfo = (LvInfo) dataDao.getFirstObjectViaParam(lhql, new String[] { "lv" }, from_user.getLevel());
					if (linfo != null && from_user.getExp() + 1 > (linfo.getExp() + linfo.getTotal())) {
						from_user.setLevel(from_user.getLevel() + 1);
					}
					Expinfo expinfo = new Expinfo();
					expinfo.setExp(1);
					expinfo.setUser_id(user_id);
					expinfo.setType(2);
					expinfo.setCreate_time(new Date());
					dataDao.addObject(expinfo);
				}
				dataDao.updateObject(from_user);
			}
			dataDao.updateObject(info);
			dataDao.updateObject(to_user);

		}
		return info;
	}

	/**
	 * 
	 * @Description 获得有图片的动态的图片
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getIndexMoment(long user_id) {
		String hql = "select pics from MomentInfo where user_id = :user_id and pics != '' and video = '' order by create_time desc";
		List<String> infos = (List<String>) dataDao.pageQueryViaParam(hql, 3, 1, new String[] { "user_id" }, user_id);
		return infos;
	}

	/**
	 * 
	 * @Description 删除动态
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteMoment(MomentInfo info) {
		dataDao.deleteObject(info);
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<MomentComment> getCommentByParams(long user_id, Integer pageIndex, int pageSize) {
		String wherehql = " where 1 = 1 ";
		if (user_id != 0) {
			wherehql += " and user_id = " + user_id + " ";
		}

		String orderhql = " order by create_time desc";
		String chql = "select count(u) from MomentComment u " + wherehql;
		String hql = " from MomentComment " + wherehql + orderhql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<MomentComment> result = new PageBodyModel<MomentComment>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<MomentComment> users = new ArrayList<MomentComment>();
			users = (List<MomentComment>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			result = new PageBodyModel<MomentComment>(page, users);
		}
		return result;
	}

	public MomentComment getCommentById(long comment_id) {
		MomentComment comment = dataDao.getObjectById(MomentComment.class, comment_id);
		return comment;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateComment(MomentComment comment) {
		dataDao.updateObject(comment);
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<MomentInfo> getMomentByParams(long user_id, Integer pageIndex, int pageSize) {
		String wherehql = " where 1 = 1 ";
		if (user_id != 0) {
			wherehql += " and user_id = " + user_id + " ";
		}
		String orderhql = " order by id desc";
		String chql = "select count(u) from MomentInfo u " + wherehql;
		String hql = " from MomentInfo " + wherehql + orderhql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<MomentInfo> result = new PageBodyModel<MomentInfo>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<MomentInfo> users = new ArrayList<MomentInfo>();
			users = (List<MomentInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			result = new PageBodyModel<MomentInfo>(page, users);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<FeedBack> getFeedbacklist(int i, Integer pageIndex, int pagesize) {
		String chql = " select count(id) from FeedBack";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<FeedBack> result = new PageBodyModel<>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, DsConstant.PAGESIZE);
			String hql = " from FeedBack order by create_time desc";
			List<FeedBack> infos = new ArrayList<>();
			infos = (List<FeedBack>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page.getPageIndex(), null);
			for (FeedBack feed : infos) {
				UserInfo user = dataDao.getObjectById(UserInfo.class, feed.getUser_id());
				if (user != null) {
					feed.setUser(user);
				}
			}
			result = new PageBodyModel<>(page, infos);
		}

		return result;
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<MomentInfo> getChioce(int i, Integer pageIndex, int pagesize) {
		String wherehql = " where 1 = 1 and chioce = 1";
		String orderhql = " order by top_time desc";
		String chql = "select count(u) from MomentInfo u " + wherehql;
		String hql = " from MomentInfo " + wherehql + orderhql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<MomentInfo> result = new PageBodyModel<MomentInfo>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pagesize);
			List<MomentInfo> users = new ArrayList<MomentInfo>();
			users = (List<MomentInfo>) dataDao.pageQueryViaParam(hql, pagesize, page.getPageIndex(), null);
			result = new PageBodyModel<MomentInfo>(page, users);
		}
		return result;
	}

	/**
	 * 评论加经验
	 * 
	 * @param uid
	 * @param momid
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addexpBycomment(long uid, long momid) {
		String exphql = "select count(exp) from Expinfo where user_id = :user_id and DATE_FORMAT(NOW(),'%Y-%m-%d') = DATE_FORMAT(create_time,'%Y-%m-%d') " + "and type = 4";
		String momhql = "from MomentComment where user_id = :user_id and moment_id = :moment_id and DATE_FORMAT(NOW(),'%Y-%m-%d') = DATE_FORMAT(create_time,'%Y-%m-%d')";

		MomentComment comment = (MomentComment) dataDao.getFirstObjectViaParam(momhql, new String[] { "user_id", "moment_id" }, uid, momid);
		long exp = (long) dataDao.getFirstObjectViaParam(exphql, new String[] { "user_id" }, uid);
		if (exp < 5 && comment == null) {
			UserInfo user = dataDao.getObjectById(UserInfo.class, uid);
			if (user != null) {
				user.setExp(user.getExp() + 1);
				String hql = " from LvInfo where lv = :lv ";
				LvInfo lvinfo = (LvInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "lv" }, user.getLevel());
				if (user.getExp() + 1 > (lvinfo.getExp() + lvinfo.getTotal())) {
					user.setLevel(user.getLevel() + 1);
				}
				Expinfo expinfo = new Expinfo();
				expinfo.setExp(1);
				expinfo.setUser_id(user.getId());
				expinfo.setType(4);
				expinfo.setCreate_time(new Date());
				dataDao.updateObject(user);
				dataDao.addObject(expinfo);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<MomentComment> getCommentListByMommentNopage(long id) {
		String hql = " from MomentComment where moment_id = :moment_id and is_delete = 0 order by create_time desc";
		List<MomentComment> list = (List<MomentComment>) dataDao.getObjectsViaParam(hql, new String[] { "moment_id" }, id);
		return list;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addMomentShare(MomentShare share) {
		dataDao.addObject(share);
	}

}
